/***************************************************************************
 *   Copyright (C) 2006 by Sergey Melderis   *
 *   sergey.melderis@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "filesprefs.h"
#include <kstaticdeleter.h>
#include <qfile.h>
  
        
FilesPrefs* FilesPrefs::m_self = 0;
static KStaticDeleter<FilesPrefs> staticDeleter;
        
FilesPrefs::FilesPrefs()
{
}

FilesPrefs::~FilesPrefs()
{
}


FilesPrefs& FilesPrefs::self()
{
    if (!m_self) { 
        staticDeleter.setObject(m_self, new FilesPrefs() );
    }
    return *m_self;
}


void FilesPrefs::setConfig(KConfig *config)
{
    m_config = config;
    readConfig();
}

QMap<QString, QString> FilesPrefs::keyUrlItems()
{
    return m_map;
}

void FilesPrefs::readConfig()
{
    QMap<QString, QString> files = m_config->entryMap("WordQuizFiles");
    QMap<QString, QString>::const_iterator iter;
    for (iter = files.constBegin(); iter != files.constEnd(); ++iter) {
        if (QFile::exists(iter.data())) {
            m_map[iter.key()] = iter.data();
        }
    }
}

void FilesPrefs::setKeyUrlItem(const QString &key, const QString &url)
{
    m_map.insert(key, url, true);
}

void FilesPrefs::removeKey(const QString &key)
{
    m_map.remove(key);
}

void FilesPrefs::writeConfig()
{
    m_config->setGroup("WordQuizFiles");
    m_config->deleteGroup("WordQuizFiles");      
    m_config->setGroup("WordQuizFiles");
    QMap<QString, QString>::const_iterator iter;
    for (iter = m_map.constBegin(); iter != m_map.constEnd(); ++iter) {
        m_config->writeEntry(iter.key(), iter.data());
    }
    m_config->sync();
}

bool FilesPrefs::hasKey(QString &key)
{
    return m_map.contains(key); 
}

bool FilesPrefs::hasKeyAndUrl(QString &key, QString &url)
{
    return (hasKey(key) && m_map[key] == url);
} 